import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Login from "../views/Login"
import Order from "../views/Order"
import Class from "../views/Class"
import User from "../views/User"
import Article from "../views/Article"
import AddArticle from "../views/article/AddArticle"
import ListArticle from "../views/article/ListArticle"
import AddClass from "../views/class/AddClass"
import ListClass from "../views/class/ListClass"
import store from "../store"

Vue.use(VueRouter)

export const menu_list = [{
        path: '/',
        name: 'Home',
        component: Home,
        title: "首页",
        icon: "el-icon-s-home",
        meta: {
            title: "首页"
        }
    },
    {
        path: "/order",
        component: Order,
        title: "订单管理",
        icon: "el-icon-shopping-cart-2",
        meta: {
            title: "订单管理"
        }
    },
    {
        path: "/user",
        component: User,
        title: "用户管理",
        icon: "el-icon-user-solid",
        meta: {
            title: "用户管理"
        }
    },
    {
        path: "/class",
        component: Class,
        title: "课程管理",
        icon: "el-icon-notebook-2",
        meta: {
            title: "课程管理"
        },
        children: [{
                path: "/class",
                component: AddClass,
                title: "添加课程",
                meta: {
                    title: "添加课程"
                }
            },
            {
                path: "/class/list",
                component: ListClass,
                title: "课程列表",
                meta: {
                    title: "课程列表"
                }
            }
        ]
    },
    {
        path: "/article",
        component: Article,
        title: "文章管理",
        icon: "el-icon-reading",
        meta: {
            title: "文章管理"
        },
        children: [{
                path: "/article",
                component: AddArticle,
                title: '添加文章',
                meta: {
                    title: "添加文章"
                }
            },
            {
                path: "/article/list",
                component: ListArticle,
                title: "文章列表",
                meta: {
                    title: "文章列表"
                }
            }
        ]
    },

]

const routes = [{
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: "请登录"
        }
    },
    ...menu_list
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    document.title = to.meta.title;

    let user_id = store.state.user.user_id;

    if (user_id) {
        if (to.path === "/login") {
            next("/")
        } else {
            next()
        }
    } else {
        if (to.path === "/login") {
            next()
        } else {
            next("/login")
        }
    }
})
export default router